package hc.bre.func

import hc.bre.cmn.RuleScriptBuilder

@groovy.transform.CompileStatic
class FunctionBuilder extends RuleScriptBuilder<FunctionDefinition>{
	@Override
	public void buildClassMember() {
		ruleDef.imports?.each { it ->
			if(!it.code) return;
			classUse(Class.forName(it.code));
		}

		ruleDef.list?.each { FunctionItem func->
			def argStr = func.args?func.args.collect({ it.code }).join(','):'';
			apdNl("public static def ${func.code}(${argStr})");
			apdNl("{");
			nested{
				apdNlBlock(func.script);
			};
			apdNl("};\n")
		};
	}

	//just for test convenience
	@Override
	public void buildClasRuleBody() {
		apdNl("switch(_func_){");
		
		nested{
			ruleDef.list?.each {  FunctionItem func ->
				apdNl("case '${func.code}':")
				def argStr = func.args?func.args.collect({ it.code }).join(','):'';
				apdNl("\t return ${func.code}(${argStr});");
			};
			
			apdNl('default :');
			apdNl("\t${result}.ret='please provide the correct function name with variable [_func_].';");
			apdNl("\tbreak;");
		};

		apdNl("};")
	};
}
